package com.kadron.currency;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KCommonDAO;
import com.kadron.entity.Currency;
import com.kadron.manager.KMessageManager;

@AutoCreate
@Name("kCurrencyManager")
@Scope(ScopeType.PAGE)
public class KCurrencyManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4184026689307942094L;

	private static final Log log = LogFactory.getLog(KCurrencyManager.class);
	
	private Currency currency; 
	
	@In
	private KCommonDAO commonDAO;
	
	@Create
	public void init() {
		Long currencyId = (Long) Component.getInstance("currencyId");
		if (currencyId == null) {
			currencyId = new Long(0);
		}
		if (currencyId > 0) {
			currency = (Currency) commonDAO.find(currencyId, Currency.class);
		} else {
			currency = new Currency();
		}
	}
	
	public String save() {
		if (currency == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save Currency currencyId="+currency.getId());
		}
		commonDAO.update(currency);
		good();
		return "currency";
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency;
	}
}
